package ro.ubbcluj.cs.damate.persistence.entities;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.List;

import static javax.persistence.CascadeType.*;
import static javax.persistence.FetchType.*;
import static ro.ubbcluj.cs.damate.utils.EntityUtils.*;

/**
 * @author Lóránd Fazakas (lorand)
 *         Date: 4/22/12
 *         Time: 1:12 AM
 */
@Table(name = "project", catalog = "damate")
@Entity
@PrimaryKeyJoinColumn(name = "parentEntity")
public class Project extends ObservedObject {

    private String name;

    @Column(name = "name", nullable = false, insertable = true, updatable = true, length = 255, precision = 0)
    @Basic
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private List<File> files;

    @OneToMany(fetch = LAZY, cascade = ALL, mappedBy = "project")
    @OrderBy("path asc")
    public List<File> getFiles() {
        return files;
    }

    public void setFiles(List<File> files) {
        this.files = files;
    }

    private List<Workspace> workspaces;

    @ManyToMany(fetch = LAZY, mappedBy = "projects", cascade = ALL)
    @OrderBy("path asc")
    public List<Workspace> getWorkspaces() {
        return workspaces;
    }

    public void setWorkspaces(List<Workspace> workspaces) {
        this.workspaces = workspaces;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Project)) return false;
        if (!super.equals(o)) return false;

        Project project = (Project) o;

        if (!name.equals(project.name)) return false;
        if (files != null ? !files.equals(project.files) : project.files != null) return false;
        if (workspaces != null ? !workspaces.equals(project.workspaces) : project.workspaces != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + name.hashCode();
        return result;
    }

    @Transient
    public String getUUID() {
        return generateUUID("" + getObservedEntityId(), getOwner().getUsername(), getName(), getDataTimeWithoutNanos(getCreationDate()));
    }
}
